<?php

namespace App\Http\Controllers\Admin;

use App\Models\Fangattr;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class FangAttrController extends Controller
{
    //房源属性列表
    public function index()
    {
        //实例化
        $model = new Fangattr();
        //获取数据
        $data= $model->getList();
        return view('admin.fangattr.index',compact('data'));
    }

    //添加房源属性 显示
    public function create()
    {
        $data = Fangattr::where('pid',0)->get();
        return view('admin.fangattr.create',compact('data'));
    }
    //文件上传
    public function upfile(Request $request){
        //默认图标
        $pic = config('up.pic');
        if($request->hasFile('file')){
            //上传
            $ret = $request->file('file')->store('','fangattr');
            $pic = '/uploads/fangattr/'.$ret;
        }
        return ['status'=>0,'url'=>$pic];
    }
    //添加房源属性 处理
    public function store(Request $request)
    {
        $this->validate($request,[
            'name' => 'required',
        ]);
        $postData = $request->except(['_token','file']);
        $postData['field_name'] = !empty($postData['field_name']) ? $postData['field_name'] : '';
        Fangattr::create($postData);
        return ['status'=>0,'msg'=>'添加属性成功'];
    }

    /**
     * Display the specified resource.
     *
     * @param  \App\Models\Fangattr  $fangattr
     * @return \Illuminate\Http\Response
     */
    public function show(Fangattr $fangattr)
    {
        //
    }

    //修改房源属性 显示
    public function edit(Fangattr $fangattr)
    {
        $data = Fangattr::where('pid',0)->get();
        return view('admin.fangattr.edit',compact('data','fangattr'));
    }

    //修改房源属性 处理
    public function update(Request $request, Fangattr $fangattr)
    {
        $this->validate($request,[
            'name' => 'required'
        ]);
        $postData = $request->except(['_token','file']);
        $postData['field_name'] = !empty($postData['field_name']) ? $postData['field_name'] : '';
        $fangattr->update($postData);
        return ['status'=>0,'msg'=> '修改属性成功'];
    }

    //删除房源属性
    public function destroy(Fangattr $fangattr)
    {
        $fangattr->delete();
        return ['status'=>0,'msg'=>'删除成功'];
    }
}
